import Vue from 'vue'
const axios = require('axios')
const isProd = process.env.NODE_ENV === 'production'

const baseURL = isProd
  ? 'https://www.fastmock.site/mock/6eec3f657e234248ea71c84149e43625/hotent-ui'
  : ''

const instance = axios.create({
  baseURL,
  timeout: 5000,
  headers: {
    'Content-Type': 'application/json;charset=UTF-8',
  },
})

instance.interceptors.request.use((config) => {
  config.headers = config.headers || {}
  !config.headers.Authorization &&
    Object.assign(config.headers, Vue.prototype.$requestConfig.header())
  return config
})

instance.interceptors.response.use((res) => {
  const { data, headers, config } = res

  if (config) {
    const { responseType } = config
    if (responseType && responseType == 'arraybuffer') {
      return { data, headers }
    }
  }

  if (headers) {
    // 附件下载，需要返回 headers
    const content = headers['content-disposition']
    if (content && content.startsWith('attachment;')) {
      return { data, headers }
    }
  }

  return data
})

export default instance
